package cn.user.Controller;




import cn.CommonPage.CommonPage;
import cn.CommonResult.CommonResult;
import cn.user.Dto.ShopRegistrationDto;
import cn.user.Vo.ResultVo;
import cn.user.domin.ShopRegistration;
import cn.user.service.ShopRegistrationService;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ShopRegistration")
@Validated
@Api(tags = "测试")
public class ShopRegistrationController {

    @Autowired
    private ShopRegistrationService service;

    @ApiOperation("分页查询")
    @RequestMapping(value = "/querylist", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<ShopRegistration>> list(@RequestBody ShopRegistrationDto dto) {
        List<ShopRegistration> orgEmpList = service.list(dto);
        return CommonResult.success(CommonPage.restPage(orgEmpList));
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody ShopRegistration dto) {
        Assert.notNull(dto.getId(),"Id不能为空");
        int count = service.update(dto);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestBody ShopRegistration dto) {
        Assert.notNull(dto.getId(),"Id不能为空");
        int count = service.delete(dto.getId());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("添加")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ShopRegistration dto) {
        int count = service.create(dto);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("jwt验证")
    @RequestMapping(value = "/token", method = RequestMethod.POST)
    @ResponseBody
    public ShopRegistration token(@RequestBody ShopRegistration dto) {
        if (StringUtil.isEmpty(dto.getId())){
            throw new ApiException("账号不能为空");
        }
       /* Assert.notNull(dto.getId(),"账号不能为空");
        Assert.notNull(dto.getShopId(),"密码不能为空")*/;
        ShopRegistration shopRegistration = service.token(dto);
        return  shopRegistration;
    }

    @ApiOperation("获取商品信息")
    @RequestMapping(value = "/ProductInformation", method = RequestMethod.GET)
    @ResponseBody
    public ResultVo ProductInformation(@RequestBody String barCode) {
        if (StringUtil.isEmpty(barCode)){
            throw new ApiException("条形码不能为空");
        }
        ResultVo barCodeVo = service.ProductInformation(barCode);
        return barCodeVo ;
    }

}
